package com.boxin.ims.modules.wechat.busi;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.boxin.framework.base.utils.DateUtils;
import com.boxin.ims.modules.momarketing.common.QRCodeUtils;
import com.boxin.ims.modules.wechat.entity.LotteryRecord;
import com.boxin.ims.modules.wechat.entity.NewsReply;
import com.boxin.ims.modules.wechat.entity.WechatMember;
import com.boxin.ims.modules.wechat.service.LotteryRecordService;
import com.boxin.ims.modules.wechat.service.WechatDaoHelper;

/**
 * @author Jakemanse
 * @date 2013-12-27
 * @function <p>注销和放弃自己的抽奖 </p>
 */
@Service
public class DropLotteryProcessor extends BusinessProcessor{
	
	@Autowired
	private LotteryRecordService lotteryRecordService;
	

	@Autowired
	private WechatDaoHelper wechatDaoHelper;
	
	
	@Override
	public ProcessMessage process() {
		WechatMember member = memberCheckAndReply();
		
		//关键词： 放弃
		if(member != null){
			String keyword = getTextMessage().getContent();	
			if("放弃".equals(keyword)){
				
				String reply ="亲,对不起,未找到您的获奖信息,谢谢您的参与!";
				Map<String,Object> params = new HashMap<String,Object>();
				params.put("userId", user.getId()+"");
				params.put("memberId", member.getId()+"");
				List<Map<String, Object> > myLtyLst = wechatDaoHelper.queryMineLotteryRecordByMember(params);
				if(myLtyLst != null && !myLtyLst.isEmpty()){
					
					if(myLtyLst.size()==1){	//单条奖品
						Map map =  myLtyLst.get(0);
						Date date = (Date) map.get("end_time");
						String validDate = DateUtils.formatDate(
								(date == null) ? new Date() : date, "MM月dd日");
						
						String title = "您有未使用的奖品【" + map.get("name") + "】";
						String desc = "亲，您有一份未使用的奖品【" + map.get("name") + "】,请于"
								+ validDate + "之前到店里凭此二维码核销使用,如果您想放弃本奖品请输入【放弃"+map.get("id")+"】，谢谢！";
						writeSingleLotteryImageToUser(title, desc,
								Long.parseLong(map.get("qrcode_id").toString()));
						return null;
						
					}else{//多条奖品
						List<NewsReply> newsList = new ArrayList<NewsReply>();
						for (Map<String, Object> row : myLtyLst) {
							NewsReply newsReply = new NewsReply();
							
							Date date = (Date) row.get("end_time");
							String validDate = DateUtils.formatDate(
									(date == null) ? new Date() : date, "MM月dd日");
							
							newsReply.setTitle("您有未使用的奖品【"+row.get("name").toString()+"】");
							newsReply.setDescription("您有未使用的奖品【"
									+ row.get("name").toString()+"】请于"+validDate+"之前到店里凭此打开的二维码到服务台核销使用,如果您想放弃本奖品请输入【放弃"+row.get("id")+"】谢谢!");
							newsReply.setPicUrl(QRCodeUtils.getQRCodePNGURL(Long
									.parseLong(row.get("qrcode_id").toString())));
							newsReply.setUrl(QRCodeUtils.getQRCodePNGURL(Long
									.parseLong(row.get("qrcode_id").toString())));
							newsList.add(newsReply);
						}

						writeQueryLotteryImageToUser(newsList); // 回复
						return null;
					}
					
				}	
				
			}else{
				keyword = keyword.replaceAll("放弃", "").trim();
				String regex ="^\\d+$";
				Pattern pattern = Pattern.compile(regex);
				Matcher matcher =  pattern.matcher(keyword);
				if(matcher.matches()){
					String lid = matcher.group();
					String reply ="对不起,未能找到代号为["+lid+"]的奖品,请确认是否输入正确,谢谢!";
					try {
						LotteryRecord record =	lotteryRecordService.get(Long.parseLong(lid));
						if(record != null ){
							if(record.getMember().getId()  == member.getId()){
								record.setStatus(LotteryRecord.STATUS_DROP);
								record.setUpdateTime(new Date());
								record.setRemarks("微会员'"+member.getName()+"'主动放弃抽奖奖品.");
								lotteryRecordService.save(record);
								reply = "亲，您已经成功放弃了您的奖品【"+record.getName()+"】,放弃后该奖品二维码电子凭证已作废无法使用,谢谢!";
								writeToUser(reply);
								return null;
							}else{
								writeToUser(reply);
								return null;
							}
						}else{
							writeToUser(reply);
							return null;
						}
						
					} catch (Exception e) {
						e.printStackTrace();
					}
					
					
				}else{
					writeToUser("您输入的格式不正确,请重新输入.如:放弃123");
					return null;
				}
			}
		}
		
		
		return null;
	}
	
	
	
	

	@Override
	public boolean isNeedBeMember() {
		return false;
	}
	
	

}
